<template>
  <div id="register">
    <RegisterUpperSlash></RegisterUpperSlash>
    <!--<div id="inp">
      <el-input v-model="name" placeholder="姓名" class="hi"></el-input><br>
      <template>
        <el-radio v-model="type" label="学生" class="hi" >学生</el-radio>
        <el-radio v-model="type" label="教师" class="hi">教师</el-radio>
      </template>
      <el-input v-model="email" placeholder="邮箱" class="hi"></el-input>
      <el-input v-model="phone" placeholder="电话" maxlength="11"
                show-word-limit class="hi" v-on:blur="checkPhone"></el-input>
      <el-input v-model="password" placeholder="密码" maxlength="20" show-password
                show-word-limit class="hi"></el-input>
      <el-input v-model="reassure" placeholder="输入以再次确认" maxlength="20" show-password
                show-word-limit class="hi" v-on:blur="checkPassword"></el-input>
      <el-button>提交</el-button>
    </div>-->


    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" id="inp">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="性别">
        <el-radio label="男" value="男" name="sex" v-model="ruleForm.sex"></el-radio>
        <el-radio label="女" value="女" name="sex" v-model="ruleForm.sex"></el-radio>
      </el-form-item>
      <el-form-item label="身份">
        <el-radio label="学生" value="学生" name="type" v-model="ruleForm.identity"></el-radio>
        <el-radio label="教师" value="教师" name="type" v-model="ruleForm.identity"></el-radio>
      </el-form-item>
      <el-form-item label="邮箱地址" prop="email">
        <el-input v-model="ruleForm.email"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="phone" >
        <el-input v-model.number="ruleForm.phone" maxlength="11" show-word-limit ></el-input>
      </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import upperSlash from "../components/RegisterUpperSlash";
import RegisterUpperSlash from "../components/RegisterUpperSlash";
import request from "../../utils/request";

export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        name:'',
        identity: '学生',
        email:'',
        phone:'',
        password:'',
        sex:'男',
        checkPass: '',
          },
      rules: {
            name: [
              {
                validator: (rule, value, callback) => {
                    var reg = new RegExp("[`~!@#$^%&*()=|{}':',\\[\\].<>《》/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？]")
                    if (reg.test(value)) {
                      callback(new Error('不能含有特殊字符'))
                    } else {
                      callback()
                    }

                },
                trigger: 'blur'
              },
              { required: true, message: '请输入姓名', trigger: 'blur' },
              { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
            ],
            email:[
              {required:true,message:'请填写邮箱',trigger:'blur'},
              {
                validator: function(rule, value, callback) {
                  if (/^([a-zA-Z0-9]+[_|_|.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|_|.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(value) === false) {
                    callback(new Error("请输入正确邮箱地址！"));
                  } else {
                    //校验通过
                    callback();
                  }
                },
                trigger: "blur"
              }
            ],
            phone:[
              { required:true,message:'请输入电话号码',trigger:'blur'},
              { type:'number',message: '电话号码输入有误',trigger:'blur'}
            ],
            password: [
              {
                validator: (rule, value, callback) => {
                  if (value.length > 18 || value.length < 6) {
                    callback(new Error('长度必须为6~18个字符'))
                  } else {
                    var reg = new RegExp("[`~!@#$^&*()_=|{}':',\\[\\].<>《》/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？]")
                    if (reg.test(value)) {
                      callback(new Error('请输入不包含非法字符的密码！'))
                    } else {
                      callback()
                    }
                  }
                },
                trigger: 'blur'
              },
              { validator: validatePass, trigger: 'blur' },
              { type: 'string',message: '请输入不包含非法字符的密码！', trigger: 'blur',
                transform(value) {
                  if (value && value.indexOf(' ') === -1&& value.indexOf('*') === -1&& value.indexOf('/') === -1&& value.indexOf('$') === -1&& value.indexOf('￥') === -1&& value.indexOf('%') === -1&& value.indexOf('\'') === -1) {
                    return value
                  } else {
                    return false
                  }
                }
              },
            ],
            checkPass: [
              { validator: validatePass2, trigger: 'blur' }
            ],
          }
      };
  },
  components:{
    RegisterUpperSlash,
    upperSlash,
  },
  methods:{
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          request.post("/api/user/register",this.ruleForm).then(res=>{
            console.log(res.msg)
            if(res.code==="-1"){
              this.$message({
                message: '用户重复注册！',
                type: 'error'
              });
            }else {
              this.$message({
                message: '注册成功！',
                type: 'success'
              });
              window.open("/",'_self');
            }
          })
        } else {
          console.log('错误提交！');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>
<style scoped>
#inp {
  margin: 50px;
  padding-left: 30%;
  padding-right: 30%;
}
.hi{
  box-sizing: content-box;
  margin-bottom: 15px;
}
</style>
